﻿#region references
using System;
using System.Drawing;
using System.IO;
#endregion

public static class Thumb
{
    public static bool ThumbnailCallback()
    { return true; }

    /*
     * Function to resize an original Image to a smaller thumbnail size.
     * 
     * @param
     * path: The path to the Image that has to be resized.
     * gameNr: The gameNr of the game from which the Image has to be loaded.
     * imageSize: The sizes of the big image of the selected game.
     */
    public static Image GetResizedImage(string path, int gameNr, string platform, int[,] imageSize)
    {
        Image thumbImg = null;

        /* Get the correct platform prefix for the selected Image */
        string pf = GetImagePlatform(platform);

        if (File.Exists(path + gameNr + "\\" + pf + ".jpg"))
        {
            Image originalImage = Image.FromFile(path + gameNr + "\\" + pf + ".jpg");
            imageSize[0, 0] = originalImage.Width;
            imageSize[0, 1] = originalImage.Height;

            thumbImg = originalImage.GetThumbnailImage(80, 113,
                        new Image.GetThumbnailImageAbort(ThumbnailCallback), IntPtr.Zero);
        }

        /* Check if the thumbnail already exists and save it to disk */
        if (!File.Exists(path + gameNr + "\\" + pf + "_thumb.jpg") && thumbImg != null) 
        { 
            thumbImg.Save(path + gameNr + "\\" + pf + "_thumb.jpg"); 
        }

        return thumbImg;
    }

    /*
     * Function to determine the Game platform so the correct Image is loaded from the upload folder.
     * 
     * @param
     * platform: The platform for the selected Game.
     * 
     * @returns
     * The selected platform prefix for the Image that has to be loaded.
     */ 
    public static string GetImagePlatform(string platform)
    {
        switch (platform)
        {
            case "PC":
                return "pc";
            case "Xbox 360":
                return "xbox";
            case "Nintendo Wii":
                return "wii";
            case "Nintendo DS":
                return "ds";
            case "Playstation 2":
                return "ps2";
            case "Playstation 3":
                return "ps3";
            case "Playstation Portable":
                return "psp";
            default:
                return "pc";
        }
    }
}
